package parser

import (
	"fmt"
	"gitee.com/zfd81/dbrs/sql"

	"github.com/shopspring/decimal"
	"github.com/spf13/cast"
	"vitess.io/vitess/go/vt/sqlparser"
)

func convertSet(s *sqlparser.Set, query string) (Statement, error) {
	expr := s.Exprs[0]
	switch v := expr.Expr.(type) {
	case *sqlparser.Literal:
		stmt := &SetStatement{
			Name: expr.Name.String(),
		}
		stmt.sql = query
		switch v.Type {
		case sqlparser.IntVal:
			stmt.Value = cast.ToInt(v.Val)
		case sqlparser.DecimalVal:
			stmt.Value, _ = decimal.NewFromString(v.Val)
		default:
			stmt.Value = v.Val
		}
		return stmt, nil
	case *sqlparser.NullVal:
		return &SetStatement{
			Name:  expr.Name.String(),
			Value: nil,
		}, nil
	case *sqlparser.ColName:
		return nil, fmt.Errorf("unsupported syntax: value cannot be a column, near '%v'", v.Name)
	}
	return nil, nil
}

type SetStatement struct {
	BaseStatement
	Name  string
	Value interface{}
}

func (s *SetStatement) Convert() sql.Node {
	return nil
}

func (s *UseStatement) Convert() sql.Node {
	return nil
}
